<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="terrainExaggeration" :max="2" /></div> -->
      <!-- <div class="slider-box-wrapper"><slider-box v-model:value="relativeHeight" :max="10000" /></div> -->
      <div class="select-box-wrapper"><select-box :options="layerOptions" /></div>
      <!-- <div class="select-box-wrapper"><select-box :options="entityToolbarOptions" /></div>
      <div class="select-box-wrapper"><select-box :options="sizeOptions" /></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="shadows">shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="softShadows">soft shadows</a-checkbox></div>
      <div class="select-box-wrapper"><a-checkbox v-model:checked="terrainShadows">terrain shadows</a-checkbox></div> -->
      <div class="select-box-wrapper"><a-checkbox v-model:checked="picking">选择</a-checkbox></div>
      <div id="menu">
        <!-- <p><button @click="onClick('play')">播放</button></p> -->
        <!-- <p><button @click="onClick('stop')">暂停</button></p> -->
      </div>
    </div>
  </div>
</template>

<script>
//参考：https://sandcastle.cesium.com/?src=Polylines%20on%203D%20Tiles.html&label=All
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import SliderBox from '@components/widgets/slider-box';
import SelectBox from '@components/widgets/select-box';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'pipeline', // 地下管线
  components: { DownOutlined, EditOutlined, SliderBox, SelectBox },
  setup() {
    return {
      viewer: null,
      doPicking: () => {}
    };
  },
  data() {
    return {
      layerOptions: [],
      nowPostion: {},
      selectedFeature: null,
      picking: true
    };
  },
  computed: {},
  watch: {
    picking(val) {
      this.viewer && this.doPicking(val);
    }
  },
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    initEffects() {
      let vm = this;
      const viewer = this.viewer;
      var scene = viewer.scene;
      var globe = scene.globe;

      var powerplant = scene.primitives.add(
        new Cesium.Cesium3DTileset({
          url: Cesium.IonResource.fromAssetId(8564),
          show: false
        })
      );
      var pipes = viewer.entities.add({
        polyline: {
          positions: Cesium.Cartesian3.fromDegreesArray([-76.36053390920833, 34.949935893493596, -76.36055481641581, 34.94993589886988, -76.36055477047704, 34.94992280693651]),
          width: 6,
          material: new Cesium.PolylineDashMaterialProperty({
            color: Cesium.Color.YELLOW,
            dashLength: 20.0
          }),
          show: false,
          clampToGround: true,
          classificationType: Cesium.ClassificationType.CESIUM_3D_TILE
        }
      });

      var building = viewer.scene.primitives.add(
        new Cesium.Cesium3DTileset({
          url: Cesium.IonResource.fromAssetId(40866)
        })
      );
      var route = viewer.entities.add({
        polyline: {
          positions: Cesium.Cartesian3.fromDegreesArray([
            -75.59604807301078,
            40.03948512841901,
            -75.59644577413066,
            40.039316280505446,
            -75.59584544997564,
            40.03846271524258,
            -75.59661425371488,
            40.03814087821916,
            -75.59664726332451,
            40.03818297772907
          ]),
          width: 6,
          material: new Cesium.PolylineDashMaterialProperty({
            color: Cesium.Color.YELLOW
          }),
          show: false,
          clampToGround: true,
          classificationType: Cesium.ClassificationType.CESIUM_3D_TILE
        }
      });

      this.layerOptions = ([
        {
          text: 'BIM',
          onselect: function() {
            building.show = false;
            route.polyline.show = false;
            powerplant.show = true;
            pipes.polyline.show = true;
            scene.camera.setView({
              destination: new Cesium.Cartesian3(1234151.4883992162, -5086036.79436967, 3633328.4278331124),
              orientation: {
                heading: 5.593695742186853,
                pitch: -1.0786797635545216,
                roll: 6.27892466154778
              }
            });
          }
        },
        {
          text: 'Photogrammetry',
          onselect: function() {
            building.show = true;
            route.polyline.show = true;
            powerplant.show = false;
            pipes.polyline.show = false;
            scene.camera.setView({
              destination: new Cesium.Cartesian3(1216596.5376729995, -4736445.416889214, 4081406.990364228),
              orientation: {
                heading: 5.153176564030707,
                pitch: -0.9701972964526693,
                roll: 6.277883257569513
              }
            });
          }
        }
      ]);

      this.layerOptions[0].onselect()
    },

    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.cs;

      let osm = new Cesium.OpenStreetMapImageryProvider({
        url: 'https://a.tile.openstreetmap.org/'
      });
      let viewer = new Cesium.Viewer('cesium-drawer', {
        // imageryProvider: osm,
        // terrainProvider: Cesium.createWorldTerrain(),
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        selectionIndicator: false,
        // shadows: true,
        // terrainShadows: Cesium.ShadowMode.ENABLED,
        animation: false, //是否显示动画控件
        baseLayerPicker: false, //是否显示图层选择控件
        geocoder: false, //是否显示地名查找控件
        timeline: false, //是否显示时间线控件
        sceneModePicker: false, //是否显示投影方式控件
        navigationHelpButton: false, //是否显示帮助信息控件
        infoBox: false, //是否显示点击要素之后显示的信息
        fullscreenButton: false,
        shouldAnimate: true
      });

      // viewer.scene.globe.depthTestAgainstTerrain = true;

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },

    onClick(value) {
      switch (value) {
        case 'play':
          this.stratPlay();
          break;
        case 'stop':
          this.stopPlay();
          break;
      }
    },

    stratPlay() {
      const viewer = this.viewer;
      if (((viewer.clock.shouldAnimate = !0), this.stopTime)) {
        viewer.clock.currentTime = this.stopTime;
      } else {
        // var e = '2019-06-18';
        const t = new Date(),
          i = '00',
          a = '24',
          r = new Date(new Date(t).setHours(Number(i))),
          o = new Date(new Date(t).setHours(Number(a)));
        (viewer.scene.globe.enableLighting = !0),
          (viewer.shadows = !0),
          (viewer.clock.startTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.currentTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.stopTime = Cesium.JulianDate.fromDate(o)),
          (viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP),
          (viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER),
          (viewer.clock.multiplier = 1600);
      }
    },

    stopPlay() {
      const viewer = this.viewer;
      (this.stopTime = viewer.clock.currentTime), (viewer.clock.shouldAnimate = !1);
    }
  }
});
</script>

<style lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    .cesium-viewer-bottom {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}

.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 18em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }

  .select-box-wrapper {
    padding: 5px 15px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
    width: auto;
  }
}
</style>
